package com.example.minzhengtong.ui

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.minzhengtong.Const
import com.example.minzhengtong.MyApp
import com.example.minzhengtong.R
import com.example.minzhengtong.base.BaseFg
import com.example.minzhengtong.data.BannerBean
import com.example.minzhengtong.data.UserBean
import com.example.minzhengtong.databinding.FragmentHomeBinding
import com.example.minzhengtong.ui.adapter.AdapterLike
import com.example.minzhengtong.utils.SpUtil
import com.example.minzhengtong.utils.WUtils
import com.gyf.immersionbar.ImmersionBar
import com.makeramen.roundedimageview.RoundedImageView
import com.youth.banner.util.BannerUtils
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder

/**
 * created by WY on 2021/5/27.
 */
class HomeFG : BaseFg() {

    companion object {
        @JvmStatic
        fun newInstance(param1: String = "", param2: String = "") =
            HomeFG().apply {
                arguments = Bundle().apply {
                }
            }
    }

    private lateinit var binding: FragmentHomeBinding

    private val mData = mutableListOf<BannerBean>()
    private val mAdapter by lazy {
        HomeAdapter1()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHomeBinding.inflate(inflater)
        return binding.root
    }

    private val mBanner = mutableListOf(R.drawable.banner1, R.drawable.banner2,R.drawable.banner3)

    override fun initView() {

        ImmersionBar.with(this).statusBarDarkFont(false).titleBar(binding.titleBar).init()

        binding.social.setOnClickListener {
            if (MyApp.isAuth) {
                SocialInsuranceA.enterThis(baseContext)
//                SocialInsuranceC.enterThis(baseContext)
            } else {
                AuthA.enterThis(baseContext, 0)
            }
        }

        binding.medicial.setOnClickListener {
            if (MyApp.isAuth) {
                MedicalInsuranceA.enterThis(baseContext)
            } else {
                AuthA.enterThis(baseContext, 1)
            }
        }

        mData.add(BannerBean(R.mipmap.tu5, "疫情防控专\n题"))
        mData.add(BannerBean(R.mipmap.tu6, "疫情防控诉\n求建议"))
        mData.add(BannerBean(R.mipmap.ylfw, "医疗服务"))
        mData.add(BannerBean(R.mipmap.jtwfyycl, "交通违法预\n约处理"))
        mData.add(BannerBean(R.mipmap.ylfw, "个人交通查询"))
//        mData.add(BannerBean(R.mipmap.jkzn, "福建八闽健\n康码应用…"))
//        mData.add(BannerBean(R.mipmap.home_cxjm, "城乡居民养\n老待遇资…"))
//        mData.add(BannerBean(R.mipmap.zq, "政企直通车"))
//        mData.add(BannerBean(R.mipmap.ylfw, "招投标监管\n平台"))
//        mData.add(BannerBean(R.mipmap.ylfw, "汽车购车补\n贴"))

        //最新服务
        binding.recyclerView1.apply {
            layoutManager = LinearLayoutManager(baseContext, LinearLayoutManager.HORIZONTAL, false)
            adapter = mAdapter
        }
        binding.toSb.setOnClickListener {
            startActivity(Intent(activity, SocialInsuranceA::class.java))
//            startActivity(Intent(activity, SocialInsuranceC::class.java))
        }
        //市县旗舰店
        binding.recyclerView2.apply {

        }

        //专题服务
        binding.recyclerView3.apply {

        }

        //部门服务
        binding.banner.apply {
            setDatas(mBanner)
            adapter = BannerAdapter()
//            setIndicator(CircleIndicator(baseContext))
//            indicator.indicatorView
        }

        if (SpUtil.getInstance().existKey(Const.USER_BEAN)) {
            val bean = SpUtil.getInstance().getObject(Const.USER_BEAN) as UserBean
            bean?.data?.apply {
                if (city!!.isNotEmpty()) {
                    binding?.address?.text = city
                }
                if (djje.isNotEmpty()) {
                    binding?.temp?.text = "${djje}℃"
                }
            }
        }

        binding.tagLayout.apply {
            adapter = AdapterLike(
                baseContext, mutableListOf(
                    "掌上南平",
                    "燃气公众安全",
                    "境外入闽健康码",
                    "i宁德",
                    "便民缴费"
                )
            )
        }

        binding.gjj.setOnClickListener {
            if (MyApp.isAuth) {
                GJJServiceA.enterThis(baseContext)
            } else {
                AuthA.enterThis(baseContext, 9)
            }
        }

    }

    override fun initListener() {

    }

    override fun lazyLoad() {

    }

    private inner class BannerAdapter :
        com.youth.banner.adapter.BannerAdapter<Int, ImageHolder>(mBanner) {
        override fun onCreateHolder(parent: ViewGroup, viewType: Int): ImageHolder {
            val rootLayout = BannerUtils.getView(parent, R.layout.item_banner) as LinearLayout
            return ImageHolder(rootLayout)
        }

        override fun onBindView(holder: ImageHolder, data: Int, position: Int, size: Int) {
            holder.imageView.setImageResource(data)
        }
    }

    inner class ImageHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView: RoundedImageView

        init {
            imageView = view.findViewById<View>(R.id.img_banner) as RoundedImageView
        }
    }

    private inner class HomeAdapter1 : CommonAdapter<BannerBean>(
        baseContext,
        R.layout.item_home,
        mData
    ) {
        override fun convert(holder: ViewHolder?, t: BannerBean, position: Int) {
            try {
                holder?.setImageResource(R.id.icon, t.logo)
            } catch (e: Exception) {
                e.printStackTrace()
                holder?.setImageResource(R.id.icon, 0)
            }
            holder?.setText(R.id.title, t.title)
            holder?.itemView?.layoutParams?.width = (WUtils.getScreenWidth(baseContext) / 5 + 40)
            holder?.itemView?.setOnClickListener {
            }
        }
    }

}